using System;

namespace MT5Trade.Models
{
    /// <summary>
    /// 组合盈亏计算结果
    /// </summary>
    public class CombinedPnLResult
    {
        /// <summary>
        /// 计算时间
        /// </summary>
        public DateTime CalculationTime { get; set; }

        /// <summary>
        /// 对冲对标识
        /// </summary>
        public string PairId { get; set; }

        /// <summary>
        /// 主账号盈亏（美元）
        /// </summary>
        public double MainPnL { get; set; }

        /// <summary>
        /// 从账号盈亏（美元）
        /// </summary>
        public double FollowPnL { get; set; }

        /// <summary>
        /// 盈利账号盈亏（美元）
        /// </summary>
        public double ProfitAccountPnL { get; set; }

        /// <summary>
        /// 亏损账号盈亏（美元）
        /// </summary>
        public double LossAccountPnL { get; set; }

        /// <summary>
        /// 滑点成本（美元）
        /// </summary>
        public double SlippageCost { get; set; }

        /// <summary>
        /// 点差成本（美元）
        /// </summary>
        public double SpreadCost { get; set; }

        /// <summary>
        /// 总交易成本（美元）
        /// </summary>
        public double TotalCost { get; set; }

        /// <summary>
        /// 净盈亏（美元）
        /// </summary>
        public double NetPnL { get; set; }

        /// <summary>
        /// 目标金额（美元）
        /// </summary>
        public double TargetAmount { get; set; }

        /// <summary>
        /// 应用的安全系数
        /// </summary>
        public double SafetyFactor { get; set; }

        /// <summary>
        /// 调整后的成本（考虑安全系数）
        /// </summary>
        public double AdjustedCost { get; set; }

        /// <summary>
        /// 是否达到平仓条件
        /// </summary>
        public bool ShouldClose { get; set; }

        /// <summary>
        /// 距离目标的差额（美元）
        /// </summary>
        public double DistanceToTarget { get; set; }

        /// <summary>
        /// 盈利账号标识（"Main" 或 "Follow"）
        /// </summary>
        public string ProfitAccount { get; set; }

        /// <summary>
        /// 亏损账号标识（"Main" 或 "Follow"）
        /// </summary>
        public string LossAccount { get; set; }

        /// <summary>
        /// 主账号点差（点数）
        /// </summary>
        public double MainSpread { get; set; }

        /// <summary>
        /// 从账号点差（点数）
        /// </summary>
        public double FollowSpread { get; set; }

        /// <summary>
        /// 主账号滑点（点数）
        /// </summary>
        public double MainSlippage { get; set; }

        /// <summary>
        /// 从账号滑点（点数）
        /// </summary>
        public double FollowSlippage { get; set; }

        /// <summary>
        /// 每点价值（美元）
        /// </summary>
        public double TickValue { get; set; }

        /// <summary>
        /// 计算耗时（毫秒）
        /// </summary>
        public double CalculationTimeMs { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CombinedPnLResult()
        {
            CalculationTime = DateTime.Now;
            ShouldClose = false;
        }

        /// <summary>
        /// 计算净盈亏
        /// </summary>
        public void CalculateNetPnL()
        {
            // 确定盈利和亏损账号
            if (MainPnL >= FollowPnL)
            {
                ProfitAccountPnL = MainPnL;
                LossAccountPnL = FollowPnL;
                ProfitAccount = "Main";
                LossAccount = "Follow";
            }
            else
            {
                ProfitAccountPnL = FollowPnL;
                LossAccountPnL = MainPnL;
                ProfitAccount = "Follow";
                LossAccount = "Main";
            }

            // 计算总成本
            TotalCost = SlippageCost + SpreadCost;

            // 计算调整后的成本（考虑安全系数）
            AdjustedCost = TotalCost * SafetyFactor;

            // 计算净盈亏：盈利账号盈亏 - |亏损账号盈亏| - 调整后成本
            NetPnL = ProfitAccountPnL - Math.Abs(LossAccountPnL) - AdjustedCost;

            // 计算距离目标的差额
            DistanceToTarget = NetPnL - TargetAmount;

            // 判断是否达到平仓条件
            ShouldClose = NetPnL >= TargetAmount;
        }

        /// <summary>
        /// 获取详细的结果描述
        /// </summary>
        public string GetDetailedDescription()
        {
            return $"组合盈亏计算结果:\n" +
                   $"  时间: {CalculationTime:yyyy-MM-dd HH:mm:ss.fff}\n" +
                   $"  对冲对: {PairId}\n" +
                   $"  主账号盈亏: ${MainPnL:F2}\n" +
                   $"  从账号盈亏: ${FollowPnL:F2}\n" +
                   $"  盈利账号({ProfitAccount}): ${ProfitAccountPnL:F2}\n" +
                   $"  亏损账号({LossAccount}): ${LossAccountPnL:F2}\n" +
                   $"  滑点成本: ${SlippageCost:F2}\n" +
                   $"  点差成本: ${SpreadCost:F2}\n" +
                   $"  总成本: ${TotalCost:F2}\n" +
                   $"  安全系数: {SafetyFactor:F2}\n" +
                   $"  调整后成本: ${AdjustedCost:F2}\n" +
                   $"  净盈亏: ${NetPnL:F2}\n" +
                   $"  目标金额: ${TargetAmount:F2}\n" +
                   $"  距离目标: ${DistanceToTarget:F2}\n" +
                   $"  是否平仓: {(ShouldClose ? "是" : "否")}\n" +
                   $"  计算耗时: {CalculationTimeMs:F2}ms";
        }

        /// <summary>
        /// 获取简要的日志信息
        /// </summary>
        public string GetLogSummary()
        {
            return $"[{PairId}] 净盈亏:${NetPnL:F2}, 目标:${TargetAmount:F2}, " +
                   $"平仓:{(ShouldClose ? "Y" : "N")}, " +
                   $"Main:${MainPnL:F2}, Follow:${FollowPnL:F2}, Cost:${TotalCost:F2}";
        }

        public override string ToString()
        {
            return GetLogSummary();
        }
    }
}